<template>
  <div class="app-container">
    <el-form :inline="true" :model="page" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="page.name" size="small" placeholder="请输入标题" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="page.status" size="small" placeholder="处理进度">
          <el-option label="全部" value="" />
          <el-option label="申请" value="10" />
          <el-option label="接受委托" value="11" />
          <el-option label="已付款" value="12" />
          <el-option label="拍卖中" value="13" />
          <el-option label="拍卖成功" value="14" />
          <el-option label="等待入库" value="15" />
          <el-option label="入库确认" value="16" />
          <el-option label="拍卖失败" value="17" />
          <el-option label="已退款" value="18" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="fetchData">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      size="mini"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        label="ID"
        width="50"
        align="center"
      />
      <el-table-column label="标题" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image style="width: 40px; height: 40px" :src="scope.row.images" lazy :preview-src-list="[scope.row.images]">
            <div slot="error" class="image-slot" />
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="跳转" align="center" prop="url">
        <template slot-scope="scope">
          <el-link :href="scope.row.url">查看<i class="el-icon-view el-icon--right" /></el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag size="mini">{{ statusFormatter(scope.row) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="收货地址" align="center" prop="status">
        <template slot-scope="scope">
          <span size="mini">{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="虚拟仓库地址" align="center" prop="status">
        <template slot-scope="scope">
          <span size="mini">{{ scope.row.visualAddr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column class-name="status-col" label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      :hide-on-single-page="page.singlePage"
      :current-page.sync="page.current"
      :page-sizes="[5,10,20,50]"
      :page-size="page.size"
      layout="total,sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="centerDialogVisible"
      center
    >
      <!--0代拍-->
      <el-form ref="form" label-width="80px" label-position="top">
        <el-form-item label="处理进度" label-position="top">
          <el-select v-model="manager.step" placeholder="处理进度" @change="stepChange">
            <el-option
              v-for="item in options"
              :key="item.agentStep"
              :label="item.agentStepName"
              :value="item.agentStep"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="进度备注" label-position="top">
          <el-input
            v-model="manager.stepInfo"
            type="textarea"
            :rows="5"
            placeholder="备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="refresh">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { agentBuyList, merchantEdit, merchantAdd, merchantDel, agentBuyDeal, agentBuySteps } from '@/api/admin'
export default {
  data() {
    return {
      list: [],
      listLoading: true,
      centerDialogVisible: false,
      formLabelWidth: '80px',
      dialogTitle: '新增海外商家',
      options: [],
      manager: {
        id: -1,
        title: '',
        img: '',
        url: '',
        category: '',
        type: '',
        step: -1,
        stepInfo: ''
      },
      btns: [],
      page: {
        status: '',
        name: '',
        size: 10,
        current: 1,
        total: 1,
        type: 2,
        singlePage: true
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    statusFormatter(row, column) {
      switch (row.status) {
        case 20:
          return '收到货物'
        case 21:
          return '初步商检入库'
        case 22:
          return '收到客户集货指令'
        case 23:
          return '集货完成'
        case 24:
          return '发货'
        case 25:
          return '后续跟单'
        case 26:
          return '归档'
        default:
          return ''
      }
    },
    stepChange(stepId) {
      const step = this.options.filter(item => item.agentStep === stepId)[0]
      this.manager.stepInfo = step.info
    },
    itemDetail(item) {
      this.$router.push({ path: '/school/class', query: { school: item.id }})
    },
    fetchData() {
      this.listLoading = true
      agentBuyList(this.page).then(response => {
        this.list = response.body
        this.page.total = response.count
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dialogTitle = '新增海外商家'
      this.manager.id = -1
      this.manager.title = ''
      this.manager.img = ''
      this.manager.url = ''
      this.manager.category = ''
      this.manager.step = ''
      this.centerDialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '订单处理'
      this.manager.id = row.id
      this.manager.title = row.title
      this.manager.img = row.img
      this.manager.url = row.url
      this.manager.category = row.category
      this.manager.type = row.type
      this.manager.step = parseInt(row.step)
      this.manager.status = row.status
      this.centerDialogVisible = true
      agentBuySteps(row.id).then(res => {
        this.options.splice(0, this.options.length)
        res.body.forEach(item => {
          if (item.agentStep === this.manager.step) {
            this.manager.stepInfo = item.info
          }
          this.options.push(item)
        })
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该海外商家', '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        merchantDel({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleNewSave() {
      if (this.manager.id === -1) {
        merchantAdd(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      } else {
        merchantEdit(this.manager).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '海外商家编辑成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    // 获取上传图片的路径
    getUpLoadImg(val) {
      this.manager.img = val
    },
    refresh() {
      agentBuyDeal({ id: this.manager.id, status: this.manager.step, info: this.manager.stepInfo }).then(res => {
        this.fetchData()
        this.centerDialogVisible = false
        this.$message({
          message: '保存成功',
          type: 'success'
        })
      })
    },
    getBtn() {
      if (this.manager.type === 1) {
        switch (this.manager.status) {
          case 10:
            return [{ 'code': 11, 'name': '接受委托', 'id': this.manager.id }]
          case 11:
            return [{ 'code': 12, 'name': '已付款', 'id': this.manager.id }]
          case 12:
            return [{ 'code': 13, 'name': '拍卖中', 'id': this.manager.id }]
          case 13:
            return [{ 'code': 14, 'name': '拍卖成功', 'id': this.manager.id }, { 'code': 17, 'name': '拍卖失败', 'id': this.manager.id }]
          case 14:
            return [{ 'code': 15, 'name': '等待入库', 'id': this.manager.id }]
          case 15:
            return [{ 'code': 16, 'name': '入库确认', 'id': this.manager.id }]
          case 16:
            return []
          case 17:
            return [{ 'code': 18, 'name': '已退款', 'id': this.manager.id }]
          case 18:
            return []
          default:
            return [{ 'code': 10, 'name': '申请', 'id': this.manager.id }]
        }
      } else if (this.manager.type === 0) {
        switch (this.manager.status) {
          case 0:
            return [{ 'code': 1, 'name': '已报价' }]
          case 1:
            return [{ 'code': 2, 'name': '确认付款' }]
          case 2:
            return [{ 'code': 3, 'name': '待购买' }]
          case 3:
            return [{ 'code': 4, 'name': '购买确认' }]
          case 4:
            return [{ 'code': 5, 'name': '等待入库通知' }]
          case 5:
            return [{ 'code': 6, 'name': '入库确认' }]
          case 6:
            return []
          default:
            return [{ 'code': 0, 'name': '询价' }]
        }
      } else if (this.manager.type === 2) {
        switch (this.manager.status) {
          case 20:
            return [{ 'code': 21, 'name': '初步商检入库' }]
          case 21:
            return [{ 'code': 22, 'name': '收到客户集货指令' }]
          case 22:
            return [{ 'code': 23, 'name': '集货完成' }]
          case 23:
            return [{ 'code': 24, 'name': '发货' }]
          case 24:
            return [{ 'code': 25, 'name': '后续跟单' }]
          case 25:
            return [{ 'code': 26, 'name': '归档' }]
          case 26:
            return []
          default:
            return [{ 'code': 20, 'name': '收到货物' }]
        }
      }
    }
  }
}
</script>
<style>
.el-step__title{
  font-size: 5px;
}
.el-step.is-horizontal .el-step__line{
  height: 2px;
  top: 19px;
}
</style>
